class Solution {
public:
    string simplifyPath(string path) {
        vector<string> vec;
        
        int pre = 0, now = 0;
        for (int i = 1; i < path.size(); i ++ ) {
            if (path[i] == '/') {
                pre = now, now = i;
                vec.push_back(path.substr(pre + 1, now - pre - 1));
            }
        }
        vec.push_back(path.substr(now + 1));
        
        vector<string> v;
        for (auto & c : vec) {
            if (c == "") continue;
            if (c == ".") continue;
            else if (c == "..") {
                if (v.size())
                    v.pop_back();
            }
            else v.push_back(c);
        }

        string res = "";
        for (auto & c : v) 
            res = res + "/" + c;
        
        if (!v.size()) res = "/";
        return res;
    }
};
